<?php
#通知消息表，含添加好友申请
class VipModel extends PdoModel {
    protected $_name = 'vip';

    static $vip = array(
         'com.xiaonianlang.where.ContinuousMonthl' =>  array(
            'id' => 'com.xiaonianlang.where.ContinuousMonthly', //苹果那边设定的ID
            'sale' => "4.6折",
            'title' => "连续包月VIP",
            'price' => "60",
            'rate' => "78",
            'note' => "仅2元/天"
            ),
         'com.xiaonianlang.where.monthlyVIP' =>  array(
            'id' => 'com.xiaonianlang.where.monthlyVIP',
            'sale' => "",
            'title' => "月度VIP",
            'price' => "",
            'rate' => "128",
            'note' => "仅4.26元/天"
            ),
         'com.xiaonianlang.where.halfYearVIP' =>  array(
            'id' => 'com.xiaonianlang.where.halfYearVIP',
            'sale' => "",
            'title' => "半年VIP",
            'price' => "",
            'rate' => "198",
            'note' => "仅1.03元/天"
            ),
         'com.xiaonianlang.where.YearVIP' =>  array(
            'id' => 'com.xiaonianlang.where.YearVIP',
            'sale' => "限时特价",
            'title' => "年卡VIP",
            'price' => "",
            'rate' => "188",
            'note' => "仅0.51元/天"
            )
    );

    function __construct(){
        parent::__construct();
    }


    function getExprie($now_time, $vip_id){
        if(empty($now_time)){
            $now_time = time();
        }

        $year = date('Y', $now_time);
        $month = date('m', $now_time);
        $day = date('d', $now_time);
        $mem_expire = 0;
        if($vip_id == 'com.xiaonianlang.where.ContinuousMonthly' || $vip_id == 'com.xiaonianlang.where.monthlyVIP'){ //包月

            $month = $month + 1;
            if($month >= 13){
                $year = $year + 1;
                $month = 1;
            }
            $mem_expire = strtotime($year . '-' . $month . '-' . $day);

        }elseif($vip_id == 'com.xiaonianlang.where.halfYearVIP'){ //半年
            $month = $month + 6;
            if($month >= 13){
                $year = $year + 1;
                $month = $month - 12;
            }
            $mem_expire = strtotime($year . '-' . $month . '-' . $day);

        }elseif($vip_id == 'com.xiaonianlang.where.YearVIP'){ //年卡
            $year = $year + 1;
            $mem_expire = strtotime($year . '-' . $month . '-' . $day);
        }

        return $mem_expire;
    }


    #添加
    function addData($data){
        if(empty($data)){
            return false;
        }
        return $this->insert($data);
    }

    #删除
    function deleteData($id = 0){
        if(empty($id)){
            return false;
        }
        $row = $this->find("id = {$id}");
        if (empty($row)) {
            return false;
        }
        return $this->delete("id = {$id}");
    }

    #更新
    function updateDataById($data,$id){
        return $this->update($data,"id = {$id}");
    }

    #更新非邀请好友消息
    function updateIsnewByUid($data,$uid){
        return $this->update($data,"uid = {$uid} and type!=2"); //type=2表示添加好友
    }

    function setAllNewNotificationRead($uid){
        $sql = "update {$this->_name} set isnew = 1 where uid = {$uid} and isnew = 0";
        return $this->execSql($sql);
    }

    function getDataById($id){
        if(empty($id)){
            return array();
        }

        $sql = " select * from {$this->_name} where id={$id} limit 1";
        $result = $this->fetchRow($sql);

        return $result;
    }

    #基本表信息
    function getListByAdmin($page = 1, $page_size =  20, $condition = array()){
        $sql = " select * from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and (uid = {$condition['uid']} or uid=0)";
        }
        if(isset($condition['isnew'])) {
            $sql .= " and isnew = {$condition['isnew']} ";
        }

        if(isset($condition['type'])) {
            $sql .= " and type in( {$condition['type']} ) ";
        }

        $sql .= " order by created_at desc ";

        $start = ($page -1 ) * $page_size;
        $sql .= " limit {$start}, {$page_size}";

        $data = $this->fetchAll($sql);
        return $data;
    }

    #基本表信息
    function getListByAdminCount($condition = array()){
        $sql = " select count(*) as num from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid={$condition['uid']} ";
        }
        if(isset($condition['isnew'])) {
            $sql .= " and isnew = {$condition['isnew']} ";
        }
        if(isset($condition['type'])) {
            $sql .= " and type in( {$condition['type']} ) ";
        }

        if(isset($condition['page_size'])) {
            $sql .= " limit {$condition['page_size']} ";
        }

        $result = $this->fetchRow($sql);
        $num = 0;
        if(!empty($result['num'])) {
            $num = $result['num'];
        }
        return $num;
    }


}
